// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// J [author] rhyme_lph
// A [email]  rhymelph@gmail.com
// f [github] https://github.com/rhymelph

import 'dart:io';

import 'package:base_plugin/base_plugin.dart';
import 'package:camera/camera.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:finance_app/src/widgets/button/ace_close_button.dart';
import 'package:finance_app/src/widgets/picture/normal_camera.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/o_c_r_camera_provider.dart';
import 'package:flutter_svg/flutter_svg.dart';

@RouterMeta(paramName: "ocrCameraPage")
class OCRCameraPage extends StatefulWidget {
  final bool isSelect;
  final bool activeOCR;

  const OCRCameraPage({Key key, this.isSelect: false, this.activeOCR: true})
      : super(key: key);

  @override
  _OCRCameraPageState createState() => _OCRCameraPageState();
}

class _OCRCameraPageState extends State<OCRCameraPage>
    with PagePresenter<OCRCameraPage, OCRCameraProvider> {
  @override
  void initState() {
    super.initState();
    provider.isSelect = widget.isSelect;
    if (widget.activeOCR != null) {
      provider.activeOCR = widget.activeOCR;
    }
  }

  @override
  Widget get buildWidget => Scaffold(
        backgroundColor: Colors.black,
        body: Stack(
          children: [
            Center(
                child: listBuilder<FlashMode>(
                    builder:
                        (BuildContext context, FlashMode value, Widget child) =>
                            NormalCamera(
                              key: provider.cameraKey,
                              flashMode: value,
                              activeOCR: provider.activeOCR,
                            ),
                    selector: (_, __) => __.currentFlashMode)),
            listBuilder<String>(
                builder: (BuildContext context, String value, Widget child) =>
                    value == null
                        ? Container()
                        : Center(
                            child: Image.file(File(value)),
                          ),
                selector: (_, __) =>
                    __.takePathList.length > 0 && __.isSelectSingle == true
                        ? __.takePathList.first
                        : null),
            Positioned(top: 0.0, right: 0.0, left: 0.0, child: _buildTopBar()),
            Positioned(
                right: 0.0, left: 0.0, bottom: 0.0, child: _buildBottomBar()),
          ],
        ),
      );

  _buildBottomBar() => Container(
        padding: EdgeInsets.only(
          bottom: MediaQuery.of(context).padding.bottom + getDp(40),
        ),
        child: Column(
          children: [
            if (widget.isSelect != true)
              Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  listBuilder<bool>(
                      builder: (BuildContext context, bool value,
                              Widget child) =>
                          PressedOpacityButton(
                            onPressed: provider.onSelectSingle,
                            child: Text(
                              S.current.single,
                              style: TextStyle(
                                color:
                                    value == true ? aceBlueColor : Colors.white,
                                fontSize: getSp(value == true ? 18 : 16),
                                fontWeight: value == true
                                    ? FontWeight.w700
                                    : FontWeight.w400,
                                height: 36 / (value == true ? 18 : 16),
                              ),
                            ),
                          ),
                      selector: (_, __) => __.isSelectSingle),
                  SizedBox(
                    width: getDp(40),
                  ),
                  listBuilder<bool>(
                      builder: (BuildContext context, bool value,
                              Widget child) =>
                          PressedOpacityButton(
                            onPressed: provider.onSelectCombine,
                            child: Text(
                              S.current.combine,
                              style: TextStyle(
                                color:
                                    value == true ? aceBlueColor : Colors.white,
                                fontSize: getSp(value == true ? 18 : 16),
                                fontWeight: value == true
                                    ? FontWeight.w700
                                    : FontWeight.w400,
                                height: 36 / (value == true ? 18 : 16),
                              ),
                            ),
                          ),
                      selector: (_, __) => !__.isSelectSingle),
                ],
              ),
            AppSpaces.h20,
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                _buildBottomLeftBtn(),
                _buildBottomCenterBtn(),
                if (widget.isSelect != true)
                  _buildBottomRightBtn()
                else
                  Container(
                    width: getDp(31),
                    height: getDp(31),
                  )
              ],
            ),
          ],
        ),
      );

  Widget _buildBottomRightBtn() {
    return listBuilder<int>(
      selector: (_, __) =>
          __.isSelectSingle == false && __.takePathList.length > 0
              ? __.takePathList.length
              : null,
      builder: (BuildContext context, int value, Widget child) => value == null
          ? IconButton(
              icon: Icon(Icons.info_outline),
              onPressed: provider.onNavInfo,
              iconSize: getDp(30),
              color: Colors.white,
            )
          : PressedOpacityButton(
              onPressed: provider.onNavNextDetail,
              child: SvgPicture.asset(
                A.assetsImagesSettingCheckCircle,
                width: getDp(42),
                height: getDp(42),
              ),
            ),
    );
  }

  Widget _buildBottomCenterBtn() {
    return listBuilder<int>(
      selector: (_, __) =>
          __.isSelectSingle == false && __.takePathList.length > 0
              ? __.takePathList.length
              : null,
      builder: (BuildContext context, int value, Widget child) => Stack(
        alignment: Alignment.center,
        children: [
          PressedOpacityButton(
            onPressed: provider.onTakePicture,
            child: SvgPicture.asset(
              A.assetsImagesTakePicture,
              width: getDp(80),
              height: getDp(80),
            ),
          ),
          if (value != null)
            Center(
              child: Text(
                '$value',
                style: TextStyle(
                  color: aceBlackColor,
                  fontSize: getSp(20),
                  fontWeight: FontWeight.w700,
                  height: 1,
                ),
              ),
            )
        ],
      ),
    );
  }

  Widget _buildBottomLeftBtn() {
    return listBuilder<int>(
      selector: (_, __) =>
          __.isSelectSingle == false && __.takePathList.length > 0
              ? __.takePathList.length
              : null,
      builder: (BuildContext context, int value, Widget child) => value == null
          ? listBuilder<bool>(
              selector: (_, __) => __.isSelectSingle,
              builder: (BuildContext context, bool value, Widget child) => value
                  ? child
                  : IconButton(
                      onPressed: () {},
                      icon: Container(
                        width: getDp(31),
                        height: getDp(31),
                      ),
                      iconSize: getDp(30),
                    ),
              child: IconButton(
                icon: Image.asset(
                  A.assetsImagesStyleImage,
                  width: getDp(31),
                  height: getDp(31),
                  fit: BoxFit.contain,
                ),
                onPressed: provider.onNavPicture,
                iconSize: getDp(30),
                color: Colors.white,
              ))
          : Stack(
              clipBehavior: Clip.none,
              children: [
                Image.file(
                  File(
                    provider.takePathList.last,
                  ),
                  width: getDp(40),
                  height: getDp(40),
                  fit: BoxFit.cover,
                  scale: 0.1,
                ),
                Positioned(
                    top: -getDp(7),
                    right: -getDp(12),
                    child: PressedOpacityButton(
                      onPressed: provider.onDeleteLast,
                      child: SvgPicture.asset(
                        A.assetsImagesSettingRemove_1,
                        width: getDp(24),
                        height: getDp(24),
                      ),
                    )),
              ],
            ),
    );
  }

  Widget _buildTopBar() => Padding(
        padding:
            EdgeInsets.only(top: getDp(10), left: getDp(20), right: getDp(20)),
        child: Row(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            AceCloseButton(),
            listBuilder<FlashMode>(
                builder:
                    (BuildContext context, FlashMode value, Widget child) =>
                        Padding(
                          padding: EdgeInsets.only(
                            top: MediaQuery.of(context).padding.top,
                          ),
                          child: IconButton(
                            icon: getFromFlashModel(value),
                            onPressed: provider.onChangeFlash,
                            iconSize: getDp(30),
                            color: Colors.white,
                          ),
                        ),
                selector: (_, __) => __.currentFlashMode)
          ],
        ),
      );

  Widget getFromFlashModel(FlashMode mode) {
    switch (mode) {
      case FlashMode.off:
        return Image.asset(
          A.assetsImagesStyleFlash_off_1,
          width: getDp(25),
          height: getDp(25),
          fit: BoxFit.contain,
        );
        break;
      case FlashMode.always:
        return Image.asset(
          A.assetsImagesStyleFlash_on,
          width: getDp(25),
          height: getDp(25),
          fit: BoxFit.contain,
        );
      case FlashMode.auto:
        break;
      case FlashMode.torch:
        break;
    }
    return null;
  }
}
